package com.asktgapp.user.fragment;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.engineer.fragment.QuestionItemFragment;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.user.activity.MainActivity;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * time:2018/1/24
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description: 修改后的 我的问答页面
 */

public class QuestionFragment extends BaseFragment {
    @InjectView(R.id.viewpager)
    ViewPager viewPager;
    @InjectView(R.id.group)
    RadioGroup mRadioGroup;
    @InjectView(R.id.right)
    RadioButton mRight;
    @InjectView(R.id.myBack)
    TextView myBack;
    private MyPagerAdapter pagerAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragmenet_question, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().getToolbar().setVisibility(View.GONE);
        pagerAdapter = new MyPagerAdapter(getFragmentManager());

        viewPager.setAdapter(pagerAdapter);

        viewPager.setOffscreenPageLimit(2);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                int position = group.indexOfChild(group.findViewById(checkedId));
                viewPager.setCurrentItem(position);
            }
        });
//        if ( PreferencesUtil.getBoolean(getContext(), PreferencesUtil.KEY_IS_ENGINEER)) {
//            viewPager.setCurrentItem(1);
//
//        } else {
//            viewPager.setCurrentItem(0);
//
//        }

        int position = getBaseActivity().getIntent().getIntExtra("positon",0);
        viewPager.setCurrentItem(position);
        if(position==1){
            mRight.setChecked(true);
        }

        myBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getBaseActivity().onBackPressed();
            }
        });
    }

    @Override
    public void loadData() {

    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return QuestionItemFragment.newInstance(1);
                case 1:
                    return QuestionItemFragment.newInstance(0);
            }
            return null;
        }

        @Override
        public int getCount() {
            return 2;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
